package com.view.pages.worldtown.objects
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import org.papervision3d.objects.DisplayObject3D;

	public class Shops extends DisplayObject3D
	{
		public static const DOORS_CLICKED:String = "doors_clicked";
		public static const DOORS_OPENED:String = "doors_opened";
		public static const DOORS_CLOSED:String = "doors_closed";
		public static const DOORS_CLOSED_IN:String = "doors_closed_in";
		public static const DOORS_CLOSED_OUT:String = "doors_closed_out";
		public static const DOORS_CLICKED_IN:String = "doors_clicked_in";
		public static const DOORS_CLICKED_OUT:String = "doors_clicked_out";
		
		private var $loadedXML:Object;
		private var $dims:Array;
		private var $loadedImages:Object;
		public var $objects:Array;
		private var $numObjects:int;
		private var $settings:Object;
		public var $currOpenShop:int;
		public var $globalFloor:DisplayObject3D;
		
		public var $tx:Number;
		public var $tz:Number;
		
		
		public function Shops(_loadedXML:Object,_loadedImages:Object,_dims:Array,_settings:Object)
		{
			$loadedXML = _loadedXML;
			$dims = _dims;
			$loadedImages = _loadedImages;
			$numObjects = $dims.length;
			$settings = _settings;
			super();
			//init();
		}
		public function start():void
		{
			$currOpenShop = -1;
			$objects = [];
			var $mcDoors:MovieClip = new MovieClip();
			
			for(var i:int = 0;i<$numObjects;i++)
			{
				var $obj:Shop = new Shop($loadedXML,{w:5000,h:1900,d:6000,fMat:$loadedImages.wall,rMat:$loadedImages.wall,lMat:$loadedImages.wall,bMat:$mcDoors,tMat:$loadedImages.top,btMat:$loadedImages.floor,doorsW:2880,doorsH:1620},$loadedImages.doors,$dims[i],$settings,$loadedImages);
				//$obj.
				$obj.index = i;
				$obj.$floorHolder = $globalFloor;
				$obj.start();
				
				$objects.push($obj);
				$obj.addEventListener(Shop.DOORS_CLICKED,onDoorsClicked);
				//$obj.addEventListener(Shop.DOORS_OPENED,onDoorsOpened);
				$obj.addEventListener(Shop.DOORS_CLOSED,onDoorsClosed);
				addChild($obj);
			}
		}
		public function hideShops():void
		{
			for(var i:int = 0;i<$numObjects;i++)
			{
				if(i!=$currOpenShop)
				{
					removeChild($objects[i]);
				}
			}
		}
		public function showShops():void
		{
			for(var i:int = 0;i<$numObjects;i++)
			{
				if(i!=$currOpenShop)
				{
					addChild($objects[i]);
				}
			}
		}
		private function onDoorsClicked(_e:Event):void
		{
			$tx = _e.target.$tx;
			$tz = _e.target.$tz;
			overlapFix(true);
			if($currOpenShop!=-1)
			{
				closeShop($currOpenShop);
			}
			$currOpenShop = _e.target.index;
			
			if(_e.target.isIn == true)
			{
				showShops();
				dispatchEvent(new Event(DOORS_CLICKED_IN));
			}
			else
			{
				dispatchEvent(new Event(DOORS_CLICKED_OUT));
			}
		}
		private function onDoorsClosed(_e:Event):void
		{
			overlapFix(false);
			if(_e.target.isIn == true)
			{
				hideShops();
				dispatchEvent(new Event(DOORS_CLOSED_IN));
				$objects[_e.target.index].room.enableRoom();
			}
			else
			{
				dispatchEvent(new Event(DOORS_CLOSED_OUT));
				$objects[_e.target.index].room.disableRoom();
			}
			
		}
		private function closeShop(_i:int):void
		{
			$objects[_i].hide();
		}
		private function overlapFix(_useCon:Boolean):void
		{
			for(var i:int = 0;i<$numObjects;i++)
			{
				$objects[i].overlapFix(_useCon);
			}
		}
	}
}